package com.jintu.portal.mq;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.config.MqConfig;
import com.jintu.portal.pojo.mq.NotificationEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author very
 */
@Component("rabbitMqSender")
public class RabbitMqSender {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "mqConfig")
    private MqConfig mqConfig;

    public void sendMessage(NotificationEntity message) {
        String routingKey;
        List<String> notNormalDataSource = mqConfig.getNotNormalDataSource();
        if (notNormalDataSource.contains(message.getDataSourceCode())) {
            routingKey = message.getDataSourceCode();
        } else {
            routingKey = mqConfig.getNormalDataSourceRoutingKey();
        }
        rabbitTemplate.convertAndSend(mqConfig.getDataCleanExchange(), routingKey, JSONObject.toJSONString(message));
        System.out.println("sendMessage: exchange= "+ mqConfig.getDataCleanExchange()
                + ";routingKey=" + routingKey + ";message=" + JSONObject.toJSONString(message));
    }
}
